package com.zhong.beautifulimageapp.utils;

import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.Period;

/**
 * 计算获取时间差
 * 通过使用JodaTime中的Period方法来对比两个不同的时间
 * Created by 俊峰 on 2017/7/17.
 */

public class DateTimeDistance {

    public static String getTimeDistance(String CreateTime, int type){
        String timeDistance = "";
        if (type == 0){
            timeDistance = "创建于:";
        }else if (type == 1){
            timeDistance = "最后回复于:";
        }else if (type == 2){
            timeDistance = "";
        }
        DateTime now = new DateTime(CreateTime).minusHours(8);
        DateTime then = new DateTime();
        Period period = new Interval(now.toInstant(), then.toInstant()).toPeriod();
        if (period.getYears() > 0) {
            timeDistance += period.getYears() + "年前";
        }else {
            if (period.getMonths() > 0){
                timeDistance += period.getMonths() + "月前";
            }else {
                if (period.getDays() > 0){
                    timeDistance += period.getDays() + "天前";
                }else {
                    if (period.getWeeks() > 0){
                        timeDistance += period.getWeeks() + "星期前";
                    }else {
                        if (period.getHours() > 0){
                            timeDistance += period.getHours() + "小时前";
                        }else {
                            if (period.getMinutes() > 0) {
                                timeDistance += period.getMinutes() + "分钟前";
                            }else {
                                if (period.getSeconds() > 0){
                                    timeDistance += period.getSeconds() + "秒前";
                                }else {
                                    timeDistance += "刚刚";
                                }
                            }
                        }
                    }
                }
            }
        }
        if (type == 3){
            timeDistance += "创建";
        }
        return timeDistance;
    }

}
